import entityCommon
import random
import copy

class EveEntityAppSpawnMixin():
    __guid__ = 'entities.EveEntityAppSpawnMixin'

    def AppSpawn(self, generator):
        if (len(generator.spawns) < 1):
            self.LogError('CONTENT ERROR - No spawns authored on generator', generator.generatorID)
            return 
        else:
            spawn = None
            if (generator.spawnPattern == entityCommon.PATTERN_CYCLIC):
                index = (self.lastSpawnIndexByGenerator.get(generator.generatorID, len(generator.spawns)) + 1)
                if (index >= len(generator.spawns)):
                    index = 0
                spawn = generator.spawns[index]
                self.lastSpawnIndexByGenerator[generator.generatorID] = index
            else:
                spawn = random.choice(generator.spawns)
            position = generator.spawnPoint
            if (generator.spawnRadius > 0.0):
                gw = self.gameWorldService.GetGameWorld(generator.sceneID)
                position = self.FindRandomPointAtFloor(position, generator.spawnRadius, gw)
            rotation = generator.spawnRotation
            if generator.randomRotation:
                newYaw = (rotation[0] + random.randrange(-180, 180))
                rotation = (newYaw,
                 rotation[1],
                 rotation[2])
            overrides = copy.deepcopy(spawn.overrides)
            overrides = self.OverridePosition(overrides, position, rotation)
            recipe = self.entityRecipeSvc.GetTypeRecipe(spawn.typeID, overrides)
            itemID = self.GetNextSpawnID(generator.sceneID, spawn.typeID)
            scene = self.entityService.LoadEntityScene(generator.sceneID)
            spawnedEntity = self.entityService.CreateEntityFromRecipe(scene, recipe, itemID)
            scene.CreateAndRegisterEntity(spawnedEntity)
            return spawnedEntity.entityID




